package com.zzyl.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.properties.WeChatProperties;
import com.zzyl.service.WechatService2;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WechatServiceImpl2 implements WechatService2 {

    @Autowired
    private WeChatProperties properties;

    @Override
    public String getOpenid(String code) {
        // 1、发送请求调用微信接口
        //  1.1 封装请求参数
        HashMap<String, Object> params = new HashMap<>();
        params.put("appid",properties.getAppid());
        params.put("secret",properties.getSecret());
        params.put("js_code",code);
        params.put("grant_type","authorization_code");
        // 1.2 发送请求
        String body = HttpUtil.createGet("https://api.weixin.qq.com/sns/jscode2session").form(params).execute().body();
        log.info("调用微信服务器获取openId结束，响应数据：{}",body);
        // 2、解析微信响应数据
        JSONObject result = JSONUtil.parseObj(body);
        // 响应数据中不包含ipenId代表调用失败，抛出异常信息
        if(!result.containsKey("openid")){
            throw new BaseException(BasicEnum.GET_OPENID_ERROR);
        }
        return result.getStr("openid");
    }

    @Override
    public String getWxAccessToken() {
        // 1、发送请求调用微信接口
        //  1.1 封装请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("grant_type","client_credential");
        params.put("appid","wx5e642062312115f0");
        params.put("secret","791ad6e635aeef4dfbaacfa703cbb6fd");
        // 1.2 设置参数发送请求
        String body = HttpUtil.createGet("https://api.weixin.qq.com/cgi-bin/token").form(params).execute().body();
        // 2、解析微信响应数据
        JSONObject result = JSONUtil.parseObj(body);
        // 响应数据中不包含 access_token 代表调用失败，抛出异常信息
        if(!result.containsKey("access_token")){
            throw new BaseException(BasicEnum.GET_TOKEN_ERROR);
        }
        return result.getStr("access_token");
    }

    @Override
    public String gettWxPhoneNumber(String access_token, String phoneCode, String openid) {
        // 1、发送请求调用微信接口
        //  1.1 封装请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("code",phoneCode);
        params.put("openid",openid);
        // 1.2 设置参数发送请求
        String body = HttpUtil.createPost("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + access_token)
                .body(JSONUtil.toJsonStr(params)).execute().body();
        // 2、解析微信响应数据
        JSONObject result = JSONUtil.parseObj(body);
        // 响应数据中不包含 phone_info 代表调用失败，抛出异常信息
        if(!result.containsKey("phone_info")){
            throw new BaseException(BasicEnum.GET_PHONE_ERROR);
        }
        JSONObject phoneInfo = result.getJSONObject("phone_info");
        return phoneInfo.getStr("phoneNumber");
    }
}
